﻿// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'

Shader "FLL/chapter7/世界空间中的法线纹理"
{
   Properties
    {
        _Color("[漫反射颜色] color",Color)=(1,1,1,1)
        _MainTex ("[主纹理] maintex", 2D) = "white" {}
        _BumpMap("[法线纹理] Normal Map",2D)="bump"{}
        _BumpScale("[纹理凹凸程度] bumpscale",Float)=1.0
        _Specular("[高光颜色] specular",Color)=(1,1,1,1)
        _Gloss("[光泽度] gloss",Range(8.0,256))=20
    }
    SubShader
    {


        Pass
        {
            Tags{"LightMode"="ForwardBase"}

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog
            #include "Lighting.cginc"
            #include "UnityCG.cginc"

            fixed4 _Color;
            sampler2D _MainTex;
            float4 _MainTex_ST;
            sampler2D _BumpMap;
            float4 _BumpMap_ST;
            float _BumpScale;
            fixed4 _Specular;
            float _Gloss; 

            struct appdata
            {
                float4 vertex:POSITION;
                float3 normal:NORMAL;
                // TANGENT 语义用于告诉 unity 将顶点的切线方向填充到 tangent 变量中
                float4 tangent:TANGENT;
                float4 texcoord:TEXCOORD0;
            };

            struct v2f
            {
                float4 pos:SV_POSITION;
                float4 uv:TEXCOORD0;
                //切线空间到世界空间的变化矩阵 那个矩阵好像叫btn矩阵
                float4 TtoW0:TEXCOORD1;
                float4 TtoW1:TEXCOORD2;
                float4 TtoW2:TEXCOORD3;


            };


            v2f vert (appdata v)
            {
                v2f o;
                o.pos=UnityObjectToClipPos(v.vertex);

                // uv变量存储了两个纹理坐标,xy存储 _MainTex 的,zw存储 _BumpMap的
                o.uv.xy=v.texcoord.xy*_MainTex_ST.xy+_MainTex_ST.zw;
                o.uv.zw=v.texcoord.xy*_BumpMap_ST.xy+_BumpMap_ST.zw;

                //-------------------------[计算变换矩阵]---------------------------- 
                // 世界空间的顶点坐标 稍后存储在w分量
                float3 worldPos=mul(unity_ObjectToWorld,v.vertex).xyz;
                // 世界空间下的法线方向,切线方向,副法线方向
                fixed3 worldNormal=UnityObjectToWorldNormal(v.vertex).xyz;
                fixed3 worldTangent=UnityObjectToWorldDir(v.tangent.xyz);
                // cross函数:向量叉乘,v.tangent的w分量记录的是副法线的方向性 
                fixed3 worldBinormal=cross(worldNormal,worldTangent)*v.tangent.w;

                //-------------------------[矩阵数值装填]----------------------------
                o.TtoW0=float4(worldTangent.x,worldBinormal.x,worldNormal.x,worldPos.x);
                o.TtoW1=float4(worldTangent.y,worldBinormal.y,worldNormal.y,worldPos.y);
                o.TtoW2=float4(worldTangent.z,worldBinormal.z,worldNormal.z,worldPos.z);

                return o;


            }

            fixed4 frag (v2f i) : SV_Target
            {
                
                //世界空间的坐标
                float3 worldPos=float3(i.TtoW0.w,i.TtoW1.w,i.TtoW2.w);

                //光源和视线方向
                fixed3 lightDir=normalize(UnityWorldSpaceLightDir(worldPos));
                fixed3 viewDir=normalize(UnityWorldSpaceViewDir(worldPos));

                //切线空间的纹理法线
                fixed3 bump=UnpackNormal(tex2D(_BumpMap,i.uv.zw));
                bump.xy*=_BumpScale;
                bump.z=sqrt(1.0-saturate(dot(bump.xy,bump.xy)));
                //世界空间下的纹理法线 其实就是乘了个btn矩阵,再做了个归一化
                bump=normalize(half3(dot(i.TtoW0.xyz,bump),dot(i.TtoW1.xyz,bump),dot(i.TtoW2.xyz,bump)));
                //反射率
                fixed3 albedo=tex2D(_MainTex,i.uv).rgb*_Color.rgb;
                //环境光
                fixed3 ambient =UNITY_LIGHTMODEL_AMBIENT.xyz*albedo;
                //漫反射
                fixed3 diffuse=_LightColor0.rgb*albedo*max(0,dot(bump,lightDir));

                fixed3 halfDir=normalize(lightDir+viewDir);
                fixed3 specular=_LightColor0.rgb*_Specular.rgb*pow(max(0,dot(halfDir,lightDir)),_Gloss);
                return fixed4(ambient+specular+diffuse,1.0);

            }
            ENDCG
        }
    }
    FallBack "Specular"
}
